import SwiftUI
import SPGlassKit

public struct GlassToastExample: GlassExample {
    public static let componentID = "glass-toast"
    @State private var toasts = GlassToastCenter(capacity: 3)
    @State private var savedCount = 0
    @State private var restored = false

    public init() {}

    public var body: some View {
        VStack(spacing: 16) {
            GlassButton("Save draft", systemImage: "tray.and.arrow.down") {
                savedCount += 1
                // A stable identifier: saving twice replaces the toast instead of stacking a duplicate.
                toasts.show(GlassToast("Draft saved (\(savedCount))", systemImage: "checkmark.circle", id: "saved"))
            }
            GlassButton("Delete message", systemImage: "trash", role: .destructive) {
                restored = false
                toasts.show(GlassToast("Message deleted", systemImage: "trash", action: GlassToastAction("Undo") { restored = true }))
            }
            GlassButton("Show offline notice", systemImage: "wifi.slash") {
                toasts.show(.persistent("You are offline", title: "Connection", systemImage: "wifi.slash", id: "offline"))
            }
            GlassButton("Dismiss all") { toasts.dismissAll() }
            Text(restored ? "Message restored" : "\(toasts.toasts.count) toasts on screen")
                .font(.callout)
                .foregroundStyle(.secondary)
        }
        .padding()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .glassToastHost(toasts)
    }
}

#Preview("GlassToast") { GlassToastExample() }
